﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Resn.WhuPub.BIL;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateCorporateWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateCorporatePage : Page
        {
                private readonly Dictionary<string, List<string>> provinceCitys = ProvinceManager.Instance.ProvinceCitys;

                public CreateCorporatePage()
                {
                        InitializeComponent();
                }

                public String CorporateName { get; set; }

                public string CorporateLevelString { get; set; }

                public String CorporateBranchName { get; set; }

                public String CorporateBranchProvince { get; set; }

                public String CorporateBranchCity { get; set; }

                public String CorporateBranchAddress { get; set; }

                public String CorporateBranchPostcode { get; set; }

                public String CorporateBranchWebsite { get; set; }


                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        LayoutRoot.DataContext = this;

                        string[] levels = Enum.GetNames(typeof (CorporateLevel));
                        foreach (string level in levels)
                        {
                                CorporateLevelComboBox.Items.Add(level);
                        }

                        foreach (string provinceCity in provinceCitys.Keys)
                        {
                                CorporateBranchProvinceComboBox.Items.Add(provinceCity);
                        }


                        CorporateLevelComboBox.SelectedIndex = 0;
                        CorporateBranchProvinceComboBox.SelectedIndex = 0;
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(CorporateName))
                        {
                                MessageBox.Show("学校名称不能为空");
                                return;
                        }

                        if (String.IsNullOrEmpty(CorporateLevelString))
                        {
                                MessageBox.Show("学校层次不能为空");
                                return;
                        }

                        try
                        {
                                ICorporate corporate = CorporateManager.Instance.CreateCorporate(CorporateName,
                                                                                                 (CorporateLevel)
                                                                                                 Enum.Parse(
                                                                                                         typeof (
                                                                                                                 CorporateLevel
                                                                                                                 ),
                                                                                                         CorporateLevelString));
                                corporate.Insert();

                                ICorporateBranch corporateBranch = corporate.CreateCorporateBranch(CorporateBranchName,
                                                                                                   CorporateBranchAddress,
                                                                                                   CorporateBranchPostcode,
                                                                                                   CorporateBranchProvince,
                                                                                                   CorporateBranchCity,
                                                                                                   CorporateBranchWebsite);
                                corporateBranch.Insert();
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        NavigationService.Navigate(new Uri("ManageCorporatesPage.xaml", UriKind.Relative));
                }


                private void CancelButton_Click(object sender, RoutedEventArgs e)
                {
                        NavigationService.Navigate(new Uri("ManageCorporatesPage.xaml", UriKind.Relative));
                }

                private void CorporateNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
                {
                        if (!string.IsNullOrEmpty(CorporateNameTextBox.Text) &&
                            CorporateLevelComboBox.SelectedIndex != -1)
                        {
                                UpdateCorporateBranchName();
                        }
                }

                private void CorporateLevelComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                        if (!string.IsNullOrEmpty(CorporateNameTextBox.Text) &&
                            CorporateLevelComboBox.SelectedIndex != -1)
                        {
                                UpdateCorporateBranchName();
                        }
                }

                private void UpdateCorporateBranchName()
                {
                        var corporateLevel =
                                (CorporateLevel) Enum.Parse(typeof (CorporateLevel), CorporateLevelString);
                        switch (corporateLevel)
                        {
                                case CorporateLevel.公司:
                                        CorporateBranchNameTextBox.Text = CorporateNameTextBox.Text.Trim() +
                                                                          CorporateBranch.CompanySubName;
                                        break;

                                default:
                                        CorporateBranchNameTextBox.Text = CorporateNameTextBox.Text.Trim() +
                                                                          CorporateBranch.SchoolSubName;
                                        break;
                        }
                }

                private void CorporateBranchProvinceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                        if (CorporateBranchProvinceComboBox.SelectedIndex != -1)
                        {
                                CorporateBranchCityComboBox.Items.Clear();

                                foreach (
                                        string provinceCity in
                                                provinceCitys[CorporateBranchProvinceComboBox.SelectedValue.ToString()])
                                {
                                        CorporateBranchCityComboBox.Items.Add(provinceCity);
                                }
                                CorporateBranchCityComboBox.SelectedIndex = 0;
                        }
                }
        }
}